added SSCLI 1.0
[windows-sources.git] / sdk / samples / all in on code / Visual Studio 2010 / CSDynamicallyBuildLambdaExpressionWithField / DynamicCondition / ConditionLine.cs
blob4971612c9f40a5ad22c89ccf72012b45038efe6c
1 /********************************** Module Header **************************************************\
2 * Module Name: ConditionLine.cs
3 * Project: CSDynamicallyBuildLambdaExpressionWithField
4 * Copyright (c) Microsoft Corporation.
6 * The ConditionLine.cs file defines some sub-condition connection operators and some property boxes.
8 * This source is subject to the Microsoft Public License.
9 * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
10 * All other rights reserved.
12 * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
13 * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
14 * MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
15 \**************************************************************************************************/
16 using System;
17 using System.Collections;
18 using System.Collections.Generic;
19 using System.Reflection;
21 namespace DynamicCondition
23 internal partial class ConditionLine
26 internal ConditionLine()
28 InitializeComponent();
31 /// <summary>
32 /// property DataType
33 /// </summary>
34 private Type _dataType;
35 public Type DataType
37 get
39 return _dataType;
41 set
43 _dataType = value;
47 /// <summary>
48 /// Property DataSource
49 /// </summary>
50 private PropertyInfo[] _dataSource;
51 public PropertyInfo[] DataSource
53 get
55 return _dataSource;
57 set
59 _dataSource = value;
60 cmbColumn.DataSource = value;
61 cmbColumn.DisplayMember = "Name";
65 /// <summary>
66 /// Condition compare operator
67 /// </summary>
68 public DynamicCondition.DynamicQuery.Condition.Compare OperatorType
70 get
72 return ((lb.Text == "AND") ? DynamicCondition.DynamicQuery.Condition.Compare.And :
73 DynamicQuery.Condition.Compare.Or);
75 set
77 if (value != DynamicQuery.Condition.Compare.And & value != DynamicQuery.Condition.Compare.Or)
79 throw new ArgumentException("OperatorType must be \"And\" or \"Or\"");
81 lb.Text = value.ToString().ToUpper();
85 /// <summary>
86 /// Returns a Condition(Of T) which represents the criteria stored in the UserControl
87 /// </summary>
88 public DynamicCondition.DynamicQuery.Condition<T> GetCondition<T>(T dataSrc)
91 var pType = ((PropertyInfo)cmbColumn.SelectedItem).PropertyType;
93 // CheckType ensures that T and T? are treated the same
94 if (CheckType<bool>(pType))
96 return MakeCond(dataSrc, pType, chkValue.Checked);
99 else if (CheckType<DateTime>(pType))
101 return MakeCond(dataSrc, pType, dtpValue.Value);
103 else if (CheckType<char>(pType))
105 return MakeCond(dataSrc, pType, Convert.ToChar(tbValue.Text));
107 else if (CheckType<long>(pType))
109 return MakeCond(dataSrc, pType, Convert.ToInt64(tbValue.Text));
111 else if (CheckType<short>(pType))
113 return MakeCond(dataSrc, pType, Convert.ToInt16(tbValue.Text));
115 else if (CheckType<ulong>(pType))
117 return MakeCond(dataSrc, pType, Convert.ToUInt64(tbValue.Text));
119 else if (CheckType<ushort>(pType))
121 return MakeCond(dataSrc, pType, Convert.ToUInt16(tbValue.Text));
123 else if (CheckType<float>(pType))
125 return MakeCond(dataSrc, pType, Convert.ToSingle(tbValue.Text));
127 else if (CheckType<double>(pType))
129 return MakeCond(dataSrc, pType, Convert.ToDouble(tbValue.Text));
131 else if (CheckType<decimal>(pType))
133 return MakeCond(dataSrc, pType, Convert.ToDecimal(tbValue.Text));
135 else if (CheckType<int>(pType))
137 return MakeCond(dataSrc, pType, Convert.ToInt32(SimulateVal.Val(tbValue.Text)));
139 else if (CheckType<uint>(pType))
141 return MakeCond(dataSrc, pType, Convert.ToUInt32(tbValue.Text));
144 // This can only ever be String, since we filtered the types that we added to the ComboBox
145 else
147 return MakeCond(dataSrc, pType, tbValue.Text);
152 public static List<Type> typeList;
154 /// <summary>
155 /// contract behind the where keyword
156 /// </summary>
157 public static List<Type> GetSupportedTypes()
159 if (typeList == null)
161 typeList = new List<Type>();
162 typeList.AddRange(new Type[] {typeof(DateTime), typeof(DateTime?), typeof(char),
163 typeof(char?), typeof(long), typeof(long?), typeof(short), typeof(short?),
164 typeof(ulong), typeof(ulong?), typeof(ushort), typeof(ushort?), typeof(float),
165 typeof(float?), typeof(double), typeof(double?), typeof(decimal), typeof(decimal?),
166 typeof(bool), typeof(bool?), typeof(int), typeof(int?), typeof(uint), typeof(uint?),
167 typeof(string)});
170 return typeList;
173 /// <summary>
174 /// Combine condition
175 /// </summary>
176 private void ConditionLine_Load(object sender, EventArgs e)
178 cmbOperator.DisplayMember = "Name";
179 cmbOperator.ValueMember = "Value";
180 var opList = MakeList(new { Name = "Equal", Value = DynamicQuery.Condition.Compare.Equal },
181 new { Name = "Not Equal", Value = DynamicQuery.Condition.Compare.NotEqual },
182 new { Name = ">", Value = DynamicQuery.Condition.Compare.GreaterThan },
183 new { Name = ">=", Value = DynamicQuery.Condition.Compare.GreaterThanOrEqual },
184 new { Name = "<", Value = DynamicQuery.Condition.Compare.LessThan },
185 new { Name = "<=", Value = DynamicQuery.Condition.Compare.LessThanOrEqual },
186 new { Name = "Like", Value = DynamicQuery.Condition.Compare.Like });
187 cmbOperator.DataSource = opList;
190 /// <summary>
191 /// select which control to demonstrate when get property from the user picked
192 /// </summary>
193 private void cboColumn_SelectedIndexChanged(object sender, EventArgs e)
196 // Get the underlying type for the property the user picked
197 var propType = ((PropertyInfo)cmbColumn.SelectedItem).PropertyType;
199 // Display appropriate control (CheckBox/TextBox/DateTimePicker) for property type
200 if (CheckType<bool>(propType))
202 SetVisibility(true, false, false);
205 else if (CheckType<DateTime>(propType))
207 SetVisibility(false, true, false);
209 else
211 SetVisibility(false, false, true);
215 /// <summary>
216 /// Set which control is visible
217 /// </summary>
218 private void SetVisibility(bool chkBox, bool datePicker, bool txtBox)
220 chkValue.Visible = chkBox;
221 tbValue.Visible = txtBox;
222 dtpValue.Visible = datePicker;
225 /// <summary>
226 /// Toggle between AND/OR
227 /// </summary>
228 private void lblOperator_Click(object sender, System.EventArgs e)
230 lb.Text = ((lb.Text == "AND") ? "OR" : "AND");
233 /// <summary>
234 /// MakeCond Operator
235 /// </summary>
236 private DynamicCondition.DynamicQuery.Condition<T> MakeCond<T, S>(T dataSource, Type propType, S value)
238 IEnumerable<T> dataSourceType = null;
239 return DynamicCondition.DynamicQuery.Condition.Create<T>(dataSourceType, cmbColumn.Text,
240 (DynamicQuery.Condition.Compare)cmbOperator.SelectedValue, value, propType);
243 /// <summary>
244 /// Returns true if propType is of type T or Nullable(Of T)
245 /// </summary>
246 private static bool CheckType<T>(Type propType) where T: struct
248 return (propType.Equals(typeof(T)) | propType.Equals(typeof(T?)));
251 /// <summary>
252 /// Turns list of parameters into an IEnumerable(Of T) (where T is an anonymous type in this case)
253 /// </summary>
254 private static IEnumerable<T> MakeList<T>(params T[] items)
256 return items;